using System;
using HT.Framework;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

namespace TG
{

    public class Map : HTBehaviour
    {

        private static Map _instance;

        public static Map Instance
        {
            get
            {
                if (_instance == null)
                {
                    Debug.LogError($"地图尚未创建！！！");
                    return null;
                }

                return _instance;
            }
        }

        //启用自动化
        protected override bool IsAutomate => true;

        public Vector3 mapStartPos = Vector3.zero;

        public Dictionary<int, MapCell> allMapCell = new Dictionary<int, MapCell>();

        /// <summary>
        /// 可以用来创建的格子
        /// </summary>
        public List<int> canCreateItemMapCell = new List<int>();

        /// <summary>
        /// 不能使用的mapcell
        /// </summary>
        public List<MapCellArray> cantUseMapCells;

        protected override void Awake()
        {
            _instance = this;
        }

        // Start is called before the first frame update
        void Start()
        {
            InitAllMap();
        }

        private void InitAllMap()
        {
            float mapCellHalf = Default.MapCellWidth / 2f;
            int mapIndex = 0;
            for (int i = 0; i < Default.MapCellWidthNum; i++)
            {
                Vector3 mapAddPosX = new Vector3(i * Default.MapCellWidth + mapCellHalf, 0, 0);
                for (int j = 0; j < Default.MapCellWidthNum; j++)
                {
                    Vector3 mapAddPosZ = new Vector3(0, 0, j * Default.MapCellWidth + mapCellHalf);
                    Vector3 mapAddPos = mapAddPosX + mapAddPosZ;
                    mapIndex = i * Default.MapCellWidthNum + j;
                    allMapCell.Add(mapIndex, new MapCell(mapIndex, mapStartPos + mapAddPos));
                    canCreateItemMapCell.Add(mapIndex);
                }
            }
        }

        private int RandomMapCellIndex(List<int> list)
        {
            var index = Random.Range(0, list.Count);
            try
            {
                int mapIndex = list[index];
                return mapIndex;
            }
            catch (Exception e)
            {
                Debug.LogError($"创建是地形id错误：index:{index} listCount:{list.Count} " + e);
                throw;
            }
        }

        /// <summary>
        /// 随机一个放物品的格子
        /// </summary>
        /// <returns></returns>
        public MapCell RandomCreateItem()
        {
            int cellIndex = RandomMapCellIndex(canCreateItemMapCell);
            //获取不可创建25宫格
            MapCellArray mapCellArray = new MapCellArray();
            mapCellArray.InitItemCantUserMapCellArray(cellIndex);
            cantUseMapCells.Add(mapCellArray);
#if UNITY_EDITOR
            //画出9、25
            for (int i = 0; i < mapCellArray.cantUseMapCellList.Count; i++)
            {
                var mapCell = mapCellArray.cantUseMapCellList[i];
                mapCell.is25 = true;
            }

            for (int i = 0; i < mapCellArray.mapArray.Length; i++)
            {
                for (int j = 0; j < mapCellArray.mapArray[i].Length; j++)
                {
                    var mapCell = mapCellArray.mapArray[i][j];
                    if (mapCell <= -1) continue;
                    allMapCell[mapCell].is9 = true;
                }
            }
#endif
            canCreateItemMapCell.RemoveAll(item => mapCellArray.ContainsMapCell(item));
            return allMapCell[cellIndex];
        }

        /// <summary>
        /// 获取地图中某件物品的数量
        /// </summary>
        /// <param name="mapItemType"></param>
        /// <returns></returns>
        public int GetMapItemNum(MapItemType mapItemType)
        {
            int num = 0;
            foreach (var cantUseMapCell in cantUseMapCells)
            {
                int itemId = cantUseMapCell.GetMapCellType();
                if (allMapCell[itemId].GetMapCellType().Equals(mapItemType))
                {
                    num++;
                }
            }

            return num;
        }

        public void RemoveItem(int id)
        {
            MapCellArray mapCellArray = new MapCellArray();
            for (int i = cantUseMapCells.Count - 1; i >= 0; i--)
            {
                if (cantUseMapCells[i].arrayIndex == id)
                {
                    mapCellArray = cantUseMapCells[i];
                    cantUseMapCells.RemoveAt(i);
                    break;
                }
            }

            foreach (var i in mapCellArray.cantUseMapCellList)
            {
                i.ResetMapCellType();
                if (i.useNum <= 0)
                    canCreateItemMapCell.Add(i.mapItemId);
            }
        }


        public MapCell GetMapCell(int mapCellId)
        {
            if (allMapCell.ContainsKey(mapCellId))
            {
                return allMapCell[mapCellId];
            }

            return null;
        }

        public void Clear()
        {
            foreach (var mapCell in allMapCell)
                mapCell.Value.Clear();
            
            canCreateItemMapCell.Clear();
            foreach (var mapCell in allMapCell)
                canCreateItemMapCell.Add(mapCell.Key);
            
            foreach (var cantUseMapCell in cantUseMapCells)
                cantUseMapCell.Clear();
            cantUseMapCells.Clear();
        }
    }
}
